import PathConfig from "../config/PathConfig";
import { game } from "../game";
import TimeManage from "../time/TimerManage";
import Tween from "../anim/Tween";
import Utils from "../utils/Utils";
import UIManage from "./UIManage";

/**网络等待遮罩 */
export default class NetMask extends Laya.Sprite {

    static uo: NetMask;

    static show(mask: boolean = false, maskbg: boolean = false, delay: number = 200) {
        if (Utils.isNil(this.uo)) {
            let view = new NetMask();
            view.zOrder = 999;
            view.visible = false;
            game.addChild(view)
        }
        this.uo.run();
    }

    static clear() {
        if (this.uo) {
            this.uo.stop();
            this.uo.hide();
        }
    }

    private img: Laya.Image;

    private lastTime: number;                 //最后一次调用时间
    private intervalTime: number = 200;       //调用后生效时间
    private runing: boolean;                  //是否在运行

    constructor() {
        super();
        NetMask.uo = this;
    }

    onAwake(): void {
        this.loadImage(PathConfig.RES_URL + "component/tip_bg.png");
        if (!this.img) {
            let img = this.img = new Laya.Image(PathConfig.RES_URL + "common/load_Reel.png");
            img.centerX = img.centerY = 0;
            img.anchorX = img.anchorY = 0.5;
            this.addChild(img);
        }

        this.width = Laya.stage.width;
        this.height = Laya.stage.height;

        // this.on(Laya.Event.MOUSE_DOWN, this, (e: Laya.Event) => {
        //     e.stopPropagation();
        // })
    }


    run() {
        if (this.intervalTime <= 0) {
            this.show();
            return;
        }

        this.lastTime = TimeManage.I.ms;
        if (!this.runing) {
            this.timer.frameLoop(1, this, this.update);
            this.runing = true;
        }
    }

    stop() {
        this.timer.clearAll(this);
        this.runing = false;
    }

    show() {
        this.visible = true;
        // this.alpha = 0;
        this.img.rotation = 0;

        Tween.once(this.img)
            .set({ alpha: 0 })
            .to({ alpha: 1 }, 200)


        // let tween = this.imgTween = Laya.TimeLine.to(this.img, { rotation: 360 }, 500);
        Tween.get(this.img, { loop: true })
            .to({ rotation: 360 }, 500)
    }

    hide() {
        Tween.clearAll(this.img);
        this.visible = false;
    }

    update() {
        if (Laya.timer.currTimer - this.lastTime >= this.intervalTime) {
            this.show();
            this.timer.clear(this, this.update);
        }
    }
} 